﻿using LuaInterface;
using UnityEngine;
using UnityEngine.UI;

namespace Framework.UGUI
{
    public class ToggleWrap : MonoBehaviour
    {
        public static ToggleWrap Get(GameObject go)
        {
            Toggle toggle = go.GetComponent<Toggle>();
            if (toggle != null)
            {
                return go.OnceAddComponent<ToggleWrap>();
            }
            return null;
        }

        public static ToggleWrap GetWithPath(GameObject go, string path)
        {
            var toggleTransform = go.transform.Find(path);
            if (toggleTransform != null)
            {
                return Get(toggleTransform.gameObject);
            }
            return null;
        }

        private Toggle _toggle;
        public Toggle toggle
        {
            get
            {
                if (_toggle == null)
                {
                    _toggle = gameObject.GetComponent<Toggle>();
                }
                return _toggle;
            }
        }

        public bool isOn
        {
            get { return toggle.isOn; }
            set { toggle.isOn = value; }
        }

        private GeneralCallBackWithParam _onValueChangedCallback;

        public void AddOnValueChanged(LuaFunction onValueChanged, LuaTable onValueChangedTarget, object param = null)
        {
            GeneralCallBackWithParam.GetOrReuse(ref _onValueChangedCallback, onValueChanged, onValueChangedTarget, param);
            toggle.onValueChanged.RemoveListener(_OnValueChanged);
            toggle.onValueChanged.AddListener(_OnValueChanged);
        }

        public void RemoveOnValueChanged()
        {
            _clearCallBack();
            toggle.onValueChanged.RemoveListener(_OnValueChanged);
        }


        private ToggleChecker _toggleChecker;
        public ToggleChecker toggleChecker
        {
            get
            {
                if (_toggleChecker == null)
                {
                    _toggleChecker = gameObject.GetComponent<ToggleChecker>();
                    if (_toggleChecker == null)
                    {
                        Logger.LogError(name + " need <ToggleChecker> instead of <Toggle> Component while SetBlock");
                    }
                }
                return _toggleChecker;
            }
        }

        //拦截Toggle点击，回调到lua，由lua判断是否要触发
        public void AddBlockClick(LuaFunction blockClickCallback, LuaTable target, object param = null)
        {
            if (toggleChecker != null)
            {
                toggleChecker.AddBlockClick(blockClickCallback, target, param);
            }
        }

        public void RemoveBlockClick()
        {
            if (toggleChecker != null)
            {
                toggleChecker.RemoveBlockClick();
            }
        }

        //lua端调用TriggerToggle完成toggle的变更
        public void TriggerClick()
        {
            if (toggleChecker != null)
            {
                toggleChecker.TriggerClick();
            }
        }

        private void _OnValueChanged(bool on)
        {
            if (_onValueChangedCallback != null && (Application.isEditor || Input.touchCount <= 1))
            {
                GeneralCallBackWithParam.Invoke(ref _onValueChangedCallback, on);
            }
        }

        private void _clearCallBack()
        {
            GeneralCallBackWithParam.RecycleInst(ref _onValueChangedCallback);
        }

        void OnDestroy()
        {
            if (_onValueChangedCallback != null)
            {
                UIEventDebugger.LogUIEventError("ToggleWrap", gameObject);
            }
            _clearCallBack();
        }
    }
}
